package com.cn.web;

import com.cn.dao.CustomerRepository;
import com.cn.entity.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.UUID;

/**
 * Created by chenyuncong on 2018/8/2.
 */
@RestController
public class AccController {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private CustomerRepository repository;


    @RequestMapping("/probe/wp")
    @ResponseBody
    public Object wpPhp(HttpServletRequest request, HttpServletResponse response){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        System.out.println(sdf.format(new Date())+"-->prob post begin.......");
        try {
            Enumeration<String> enums = request.getParameterNames();
            while (enums.hasMoreElements()) {
                String name = enums.nextElement();
                System.out.println("data:"+name+"="+request.getParameter(name));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Customer cus = repository.findByDevcMac("ea223c1a-2789-4271-9e61-34e7b0ddea49");
//        System.out.println(cus.toString());

        Customer cus1=mongoTemplate.findOne(new Query(Criteria.where("devcMac").is("ea223c1a-2789-4271-9e61-34e7b0ddea49")), Customer.class);
        System.out.println(cus1);
        System.out.println(sdf.format(new Date())+"-->wphp post end.......");


        return "OK";
    }

    @RequestMapping("save")
    public String save(Customer cus){
//        repository.insert(cus);
        if(StringUtils.isEmpty(cus.getDevcMac())){
            cus.setDevcMac(UUID.randomUUID().toString());
        }
        if(StringUtils.isEmpty(cus.getInTime())){
            cus.setInTime(System.currentTimeMillis()+"");
        }
        if(StringUtils.isEmpty(cus.getOutTime())){
            cus.setOutTime(System.currentTimeMillis()+"");
        }
        if(StringUtils.isEmpty(cus.getProbMac())){
            cus.setProbMac(UUID.randomUUID().toString());
        }
        if(StringUtils.isEmpty(cus.getRssi())){
            cus.setRssi(100+"");
        }
        mongoTemplate.save(cus);
        System.out.println(cus.getId());
//        repository.insert(cus);
        return "OK";
    }



}
